Conditions | 2 |
Paths | 2 |
Total Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
23 | $.fn.symphonyDefaultValue = function(options) { |
||
24 | var objects = this, |
||
25 | isOn = false, |
||
26 | settings = { |
||
27 | sourceElement: '.js-defaultvalue-source', |
||
28 | sourceEvent: 'change', |
||
29 | targetEvent: 'keyup blur' |
||
30 | }; |
||
31 | |||
32 | $.extend(settings, options); |
||
33 | |||
34 | // append our namespace on the sourceEvent |
||
35 | settings.sourceEvent += '.symphony-defaultvalue'; |
||
36 | |||
37 | var source = $(settings.sourceElement); |
||
38 | |||
39 | var getTargetValue = function () { |
||
40 | return objects.val(); |
||
41 | }; |
||
42 | |||
43 | var setTargetValue = function (val) { |
||
44 | objects.val(val); |
||
45 | }; |
||
46 | |||
47 | var getSourceValue = function () { |
||
48 | return source.find('option:selected').text(); |
||
49 | }; |
||
50 | |||
51 | var sourceChanged = function (e) { |
||
52 | if (isOn) { |
||
53 | setTargetValue(getSourceValue()); |
||
54 | } |
||
55 | }; |
||
56 | |||
57 | var on = function () { |
||
58 | if (isOn) { |
||
59 | return; |
||
60 | } |
||
61 | source.on(settings.sourceEvent, sourceChanged); |
||
62 | isOn = true; |
||
63 | }; |
||
64 | |||
65 | var off = function () { |
||
66 | if (!isOn) { |
||
67 | return; |
||
68 | } |
||
69 | $(settings.sourceElement).off(settings.sourceEvent); |
||
70 | isOn = false; |
||
71 | }; |
||
72 | |||
73 | /*------------------------------------------------------------------------- |
||
74 | Initialisation |
||
75 | -------------------------------------------------------------------------*/ |
||
76 | |||
77 | objects.on(settings.targetEvent, function (e) { |
||
78 | if (!getTargetValue()) { |
||
79 | on(); |
||
80 | } |
||
81 | else { |
||
82 | off(); |
||
83 | } |
||
84 | }); |
||
85 | |||
86 | if (!getTargetValue()) { |
||
87 | on(); |
||
88 | } |
||
89 | |||
90 | /*-----------------------------------------------------------------------*/ |
||
91 | |||
92 | return objects; |
||
93 | }; |
||
94 | |||
96 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.